package com.bjsxt.controller.system;

import com.bjsxt.aspectj.annotation.Log;
import com.bjsxt.aspectj.enums.BusinessType;
import com.bjsxt.domain.Dept;
import com.bjsxt.dto.DeptDto;
import com.bjsxt.service.DeptService;
import com.bjsxt.utils.ShiroSecurityUtils;
import com.bjsxt.vo.AjaxResult;
import com.bjsxt.vo.DataGridView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;
import java.util.List;

/**
 * @author:liao
 * @Description:TODO 部门
 * @date:2020/12/3
 */
@RestController
@RequestMapping("/system/dept")
public class DeptController {

    @Autowired
    private DeptService deptService;

    /**
     * 分页查询
     */
    @GetMapping("listDeptForPage")
    public AjaxResult listDeptForPage(DeptDto deptDto){
        DataGridView dataGridView = deptService.listPage(deptDto);
        return AjaxResult.success("查询成功",dataGridView.getData(),dataGridView.getTotal());
    }

    /**
     * 查询有效的部门
     */
    @GetMapping("selectAllDept")
    public AjaxResult selectAllDept(){
        List<Dept> deptList = deptService.list();
        return AjaxResult.success(deptList);
    }

    /**
     * 根据部门id查询
     */
    @GetMapping("getDeptById/{deptId}")
    public AjaxResult getDeptById(@Validated @PathVariable @NotEmpty(message = "部门id不能为空") Long deptId){
        Dept dept = deptService.getOne(deptId);
        return AjaxResult.success(dept);
    }

    /**
     * 添加部门
     */
    @PostMapping("addDept")
    @Log(title = "部门管理",businessType = BusinessType.INSERT)
    public AjaxResult addDept(@Validated DeptDto deptDto){
        deptDto.setSimpleUser(ShiroSecurityUtils.getCurrentSimpleUser());
        return AjaxResult.toAjax(deptService.addDept(deptDto));
    }

    /**
     *修改部门信息
     */
    @PutMapping("updateDept")
    @Log(title = "部门管理",businessType = BusinessType.UPDATE)
    public AjaxResult updateDept(@Validated DeptDto deptDto){
        deptDto.setSimpleUser(ShiroSecurityUtils.getCurrentSimpleUser());
        return AjaxResult.toAjax(deptService.updateDept(deptDto));
    }

    /**
     * 删除部门信息
     */
    @Log(title = "部门管理",businessType = BusinessType.DELETE)
    @DeleteMapping("deleteDeptByIds/{deptIds}")
    public AjaxResult delete(@PathVariable @Validated @NotEmpty(message = "科室id不能为空") Long[] deptIds){
        return AjaxResult.toAjax(deptService.deleteDeptByIds(deptIds));
    }
}
